package com.gushi.POICreateExcel;

import java.lang.reflect.Field;

public class gsReflectionTool {

    public static String[] getPropNames (Object obj) {

        Field[] fields = obj.getClass().getDeclaredFields();
        String[] propNames = new String[fields.length];

        for (int i = 0; i < fields.length; ++i) {
            propNames[i] = fields[i].getName();
        }

        return propNames;
    }

    public static Object[] getPropValues (Object obj) {

        Field[] fields = obj.getClass().getDeclaredFields();
        Object[] values = new Object[fields.length];

        for (int i = 0; i <fields.length; ++i) {

            Field curField = fields[i];
            curField.setAccessible(true);

            try {
                values[i] = curField.get(obj);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }

        return values;
    }

    public static String[] getPropValueStrings (Object obj) {

        Field[] fields = obj.getClass().getDeclaredFields();
        String[] strings = new String[fields.length];

        for (int i = 0; i <fields.length; ++i) {

            Field curField = fields[i];
            curField.setAccessible(true);

            try {
                strings[i] = curField.get(obj).toString();
            } catch (IllegalAccessException e) {
                strings[i] = "";
                e.printStackTrace();
            }
        }

        return strings;
    }
}
